import xmlrpclib
from SimpleXMLRPCServer import SimpleXMLRPCServer
import bsddb
import struct
import json

class Method:
    def __init__(self):
        self.urlid = bsddb.btopen('../0.12/db.urlid','r')
        self.idurl = bsddb.btopen('../0.12/db.idurl','r')
        self.title = bsddb.btopen('../0.12/db.title','r')
        self.sitegraph = bsddb.btopen('../0.12/db.sitegraph','r')
        self.transposegraph = bsddb.btopen('../0.12/db.transposegraph','r')
        self.binfile = open('../0.12/db.webgraph','rb')
        self.statdbpath = '../0.12/db.stat'

        self.webgraph = {}
        # read webgraph
        intsize = struct.calcsize('i')
        tmp = 0
        while 1:
            data = self.binfile.read(intsize)
            if data == '':
                break
            src = str(struct.unpack('i',data)[0])

            data = self.binfile.read(intsize)
            if data == '':
                break
            outdegree = str(struct.unpack('i',data)[0])
            dest = []
            for i in range(int(outdegree)):
                data = self.binfile.read(intsize)
                if data == '':
                    break
                dest.append(str(struct.unpack('i',data)[0]))
            self.webgraph[str(src)] = dest
        self.binfile.close()
        

    def link(self,site):
        ans = {}
        urllist = []
        try:
            for pageid in self.transposegraph[self.urlid[site]].split(' '):
               urllist.append([self.idurl[pageid],self.title[pageid]])

            ans['count'] = len(urllist)
            ans['result'] = urllist
        except:
            ans['count'] = 0
            ans['result'] = []

        return json.dumps(ans)

    def site(self,site):
        ans = {}
        urllist = []
        try:
            for pageid in self.sitegraph[self.urlid[site]].split(' '):
               urllist.append([self.idurl[pageid],self.title[pageid]])

            ans['count'] = len(urllist)
            ans['result'] = urllist
        except:
            ans['count'] = 0
            ans['result'] = []

        return json.dumps(ans)

    def fwd(self,site):
        ans = {}
        ans = ''        
        if self.urlid.has_key(site) :
            siteid = self.urlid[site]
            ans += '>> URL  : '+site+'\n'
            if self.title.has_key(siteid) :
                t = self.title[siteid]
                ans += '>> Title: '+t+ '\n'
            ans += '\n'

            if self.webgraph.has_key(siteid) :
                dest = self.webgraph[siteid]
                for d in dest:
                    url = self.idurl[d]
                    ans += url +'\n'
                    if self.title.has_key(d) :
                        ans += self.title[d] + '\n'
                    ans += '----\n'
        else:
            ans += 'site not found\n'
        print ans
        return ans

    def stat(self):
        statdb = bsddb.btopen(self.statdbpath,'r')
        dict = {}
        for k in statdb.keys():
            dict[k] = statdb[k]
        return json.dumps(dict)


    def __del__(self):
        self.idurl.close()
        self.urlid.close()
        self.title.close()

def main():
    try:
        PORT = 9999
        server = SimpleXMLRPCServer(('localhost',PORT))
        print 'Listening on port %d...' % (PORT)
        server.register_instance(Method())
        server.serve_forever()
    except KeyboardInterrupt:
        print 'Closed Connection'

if __name__ == '__main__' :
    m = Method()
    #print m.link('www.kingstar.co.th.gz')
    #print m.link('http://www.kino-govno.com/comments/23940')
    #print m.site('http://www.bearshare.com/')
    main()
    #print m.stat()
